<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class citportal extends User_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     * 	- or -  
     * 		http://example.com/index.php/welcome/index
     * 	- or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    function __construct() {
        parent::__construct();
        $this->load->model("database");
    }

    public function index() {
        $captcha = $this->get_captcha();
        $this->load->view('citportal');
    }

    /**
     * Show Different Views of Page
     * 
     * Including academic view, industry view and other view
     */
    function showAcademic() {
        $this->load->view("academic");
    }

    function showIndustry() {
        
    }

    function showOthers() {
        
    }

    /**
     * Load News View
     * 
     * This method gets news data from database and load news views with data
     */
    function showNews() {
        $query = $this->database->getNews();
        $data = array();
        $i = 0;
        foreach ($query as $row) {
            $data[$i]['news'] = $row->news;
            $data[$i]['date'] = $row->date;
            $i++;
        }
        $this->load->view('news', array('data' => $data));
    }

    function showAbout() {
        $this->load->view("about");
    }

    function showTutorials() {
        $this->load->view("tutorials");
    }

    function showPeople() {
        $this->load->view("people");
    }

    function showPapers() {
        $query = $this->database->getPapers();
        $papers = $query->result_array();
        $this->load->view('papers', array('data' => $papers));
    }

    function showLinks() {
        $type = $this->input->get_post('type');
        $links = $this->database->getLinks();
        $data = $links->result_array();
        $headers = array();
        $i = 0;
        foreach ($links->result() as $link) {
            if (in_array($link->header, $headers)) {
                continue;
            }
            $headers[$i] = $link->header;
            $i++;
        }
        $this->load->view("links", array('headers' => $headers, 'data' => $data, 'type' => $type));
    }

    function showTools() {
        
    }

    function showFeedbacks() {
        $captcha = $this->get_captcha();
        $data = array(
            'name'  => "",
            'email'  => "",
            'affiliation'  => "",
            'comment'  => "",
        );
        $this->load->view("feedback", array('captcha' => $captcha, 'data' =>$data));
    }

    function submitFeedback() {
//        $this->form_validation->set_rules('name', 'name', 'trim|strip_tags|xss_clean|required');
//        $this->form_validation->set_rules('affiliation', 'affiliation', 'trim|strip_tags|xss_clean|required');
//        $this->form_validation->set_rules('email', 'email', 'trim|strip_tags|xss_clean|required|valid_email');
//        $this->form_validation->set_rules('comment', 'comment', 'trim|strip_tags|xss_clean|required');
//        $this->form_validation->set_rules('captcha', 'captcha', 'trim|strip_tags|xss_clean|callback_captcha_check|match_captcha[captcha.word]');
        $name = $this->input->get_post('name');
        $affiliation = $this->input->get_post('affiliation');
        $email = $this->input->get_post('email');
        $category = $this->input->get_post('category');
        $comment = $this->input->get_post('comment');
//        $data = array(
//            'name'  => $name,
//            'email'  => $email,
//            'affiliation'  => $affiliation,
//            'comment'  => $comment,
//        );
//        if($this->form_validation->run == false){
//            $this->load->view("feedback", array('captcha' => $captcha, 'data' => $data, 'error' => "Captcha doesn't match, re-enter captcha"));
//            return;
//        }
        $query = $this->database->insertFeedback($name, $affiliation, $email, $category, $comment);
        $From_email = $email;
//        $To_email = "wmotycha@cse.unl.edu";
        $To_email = "wangzhen614@gmail.com";
        $this->sendEmail($To_email, $From_email, $category, $affiliation, $name, $comment);
        echo $query;
        return false;
    }

//    function captcha_check($value){
//        if($value == ''){
//            $this->form_validation->set_message('captcha_check', 'Please enter the text from the image and submit your feedback');
//            return false;
//        } else {
//            return true;
//        }
//    }

    function loadTutorial(){
        $page = $this->input->get_post('page');
//        $this->load->view('tutorials');
        $this->load->view("tutorials/$page", array('start' => "1"));
    }

    function viewFile($filename, $filapath) {
        
    }

    function sendEmail($To_email, $From_email, $category, $affiliation, $name, $comment) {
            $to = "wangzhen614@gmail.com";
            $subject = 'Citportal: feedback';
            $message = 'Name: ' . ' ' . $name . "\n" 
                    .'Category: ' . ' ' . $category . "\n" 
                    . 'Email: ' . ' ' . $From_email . "\n" 
                    . 'Affiliation: ' . ' ' . $affiliation . "\n" 
                    . 'Comment: ' . " " . $comment;
            $headers = 'From: Citportal Website' . "\r\n" .
                    'X-Mailer: PHP/' . phpversion();

            mail($to, $subject, $message, $headers);
            return true;

//            return 'Temporary Password has been sent by email.';
    }

}
